package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcDemo2 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        //向userinfo中插入一条记录
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(
                //数据库地址格式：jdbc:mysql://数据库IP:端口/数据库名？一堆参数
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root");
        System.out.println("成功连接数据库");
        Statement statement=connection.createStatement();
        /*
        INSERT INTO userinfo(username,password,nickname,age)
        VALUES('张三','11','aaa',20)
         */
        String sql="INSERT INTO userinfo(username,password,nickname,age) " +
                   "VALUES('张三','11','aaa',20)";
        int a=statement.executeUpdate(sql);
        if (a>0){
            System.out.println("执行成功");
        }
        connection.close();
    }
}
